package tt.dz.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tt.dz.common.Constants;
import tt.dz.common.PublicFunc;
import tt.dz.dao.pro.UserFundCurrDao;
import tt.dz.dao.pro.UserMemberDao;
import tt.dz.dao.pro.UserPayCardDao;
import tt.dz.dao.pro.UserWalletDao;
import tt.dz.entity.UserFundCurr;
import tt.dz.entity.UserMember;
import tt.dz.entity.UserPayCard;
import tt.dz.entity.UserWallet;
import tt.dz.service.UserFundCurrService;

/**
 * 资金流水业务实现
 * @author Xielf
 *
 */
@Service
public class UserFundCurrServiceImpl implements UserFundCurrService{

	/**资金流水Dao类*/
	@Resource
	UserFundCurrDao<UserFundCurr> userFundCurrDao;
	
	/**用户账户Dao*/
	@Resource
	UserWalletDao<UserWallet> userWalletDao;
	
	/**用户支付卡操作dao*/
	@Resource
	UserPayCardDao<UserPayCard> userPayCardDao;
	
	/**用户信息操作Dao*/
	@Resource
	UserMemberDao<UserMember> userMemberDao;
	

	@Override
	public String addUserFundCurr(UserFundCurr userFundCurr,String cardNum,String ev_number) {
		
		//得到用户卡详细
		UserPayCard userPayCard = userPayCardDao.findUserPayCardByCardNum(cardNum);
		if(userPayCard==null){
			return Constants.JsonResponseCode.FAIL;
		}

		Integer userId = userPayCard.getUser_id();
		//得到用户,电子钱包详细
		UserMember userMember = userMemberDao.findUserMemberByUserId(userId);
		UserWallet userWallet = userWalletDao.findUserWalletByUserId(userId);
		if(userWallet==null || userMember==null){
			return Constants.JsonResponseCode.FAIL;
		}

		//得到动作
		Integer dealAction = userFundCurr.getDeal_action();
		//定义APP服务器回调地址
		String preCallBackUrl = "";
		//判断动作
		switch (dealAction) {
			//充值收入
		case 101:
			userWallet.setBalance(userWallet.getBalance()+userFundCurr.getDeal_income());
			break;
			//交易收入
		case 102:
			userWallet.setBalance(userWallet.getBalance()+userFundCurr.getDeal_income());
			break;
			//取消预约系统退款收入
		case 103:
			userWallet.setBalance(userWallet.getBalance()+userFundCurr.getDeal_income());
			preCallBackUrl = Constants.callbackAddr.resevation;
			break;
			//开始充电
		case 104:
			userWallet.setBalance(userWallet.getBalance()+userFundCurr.getDeal_income());
			preCallBackUrl = Constants.callbackAddr.recharge;
			break;
			//充电支出
		case 201:
			userWallet.setBalance(userWallet.getBalance()-userFundCurr.getDeal_expend());
			preCallBackUrl = Constants.callbackAddr.recharge;
			break;
			//预约支出
		case 202:
			userWallet.setBalance(userWallet.getBalance()-userFundCurr.getDeal_expend());
			preCallBackUrl = Constants.callbackAddr.resevation;
			break;
			//离线桩扣费
		case 301:
			userWallet.setBalance(userWallet.getBalance()-userFundCurr.getDeal_expend());
			break;
		default:
			break;
		}
		//修改钱包信息
		userWalletDao.update(userWallet);
		userFundCurr.setUpdate_time(new Date());
		userFundCurr.setDeal_currt_num(userMember.getUsername()+(System.currentTimeMillis()+"").substring(0, 10));
		userFundCurr.setTrade_balance(userWallet.getBalance());
		userFundCurr.setUser_id(userPayCard.getUser_id());
		userFundCurrDao.save(userFundCurr);
		//推送，屏蔽
		/**switch (dealAction) {
			//系统退款(取消预约)收入
		case 102:
			PublicFunc.getRequestByUrl(preCallBackUrl+"&user_id="+userId+"&user_card="+cardNum+"&dianzhuanghao="+ev_number+"&bespeak=0");
			break;
			//开始充电
		case 104:
			PublicFunc.getRequestByUrl(preCallBackUrl+"&user_id="+userId+"&user_pay_card="+cardNum+"&dianzhuanghao="+ev_number+"&charge=1");
			break;
			//充电支出
		case 201:
			PublicFunc.getRequestByUrl(preCallBackUrl+"&user_id="+userId+"&user_pay_card="+cardNum+"&dianzhuanghao="+ev_number+"&charge=0");
			break;
			//预约支出
		case 202:
			PublicFunc.getRequestByUrl(preCallBackUrl+"&user_id="+userId+"&user_card="+cardNum+"&dianzhuanghao="+ev_number+"&bespeak=1");
			break;
		}*/
		
		return Constants.JsonResponseCode.SUCCESS;
	}

	@Override
	public List<UserFundCurr> getUserFundCurrByUserId(Integer userId,Integer pageNo,Integer pageSize) {
		//直接返回Dao查询结果
		return userFundCurrDao.findUserFundCurrListByUserId(userId,pageNo,pageSize);
	}
	public static void main(String[] args) {

		System.out.println(PublicFunc.getRequestByUrl(Constants.callbackAddr.resevation+"&user_id=2&user_card="+123+"&dianzhuanghao="+123+"&bespeak=1"));
	}

	@Override
	public UserFundCurr getUserFundCurrByDealNum(String dealCurrNum) {
		return null;
	}
}
